﻿/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

using Daidalos.Patterns;

namespace Daidalos.Utilities
{
    /// <summary>
    ///     A logmanager that takes care of, have a guess,
    ///     logging messages into a logfile for debugging
    ///     purposes.
    /// </summary>
    public sealed class LogManager : SingletonManager<LogManager>
    {

        #region Members
        /// <summary>
        ///     Store a list of the active logging devices.
        /// </summary>
        private System.Collections.Generic.LinkedList<Interfaces.Log> logs = null;

        /// <summary>
        /// 
        /// </summary>
        private System.Collections.Generic.LinkedList<string> history = new System.Collections.Generic.LinkedList<string>();

        /// <summary>
        /// 
        /// </summary>
        private int historySize = 20;
        #endregion

        #region Accessors
        /// <summary>
        /// 
        /// </summary>
        public System.Collections.Generic.LinkedList<string> History
        {
            get
            {
                return history;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public int HistorySize
        {
            get
            {
                return historySize;
            }
            set
            {
                historySize = value;
            }
        }
        #endregion

        #region Constructors
        /// <summary>
        ///     Creates an object.
        /// </summary>
        public LogManager()
        {
        }
        #endregion

        #region Methods
        /// <summary>
        ///     This method adds a supplied logging device to the
        ///     list of active logging devices.
        /// </summary>
        /// <param name="log">
        ///     The logging device to be added to the list.
        /// </param>
        public void AddLog(Interfaces.Log log)
        {
            if (log == null)
            {
                throw new Exception("The log-manager tried to enlist an invalid logging device!");
            }
            if (logs == null)
            {
                logs = new System.Collections.Generic.LinkedList<Interfaces.Log>();
            }
            logs.AddLast(log);
        }

        /// <summary>
        ///     Invoking this method removes all enlistet logging delegates
        ///     from the list.
        /// </summary>
        public void FlushLogs()
        {
            logs = null;
        }

        /// <summary>
        /// Logs the message into the logfile
        /// </summary>
        /// <param name="message"></param>
        public void Log(string message)
        {
            // Prepend date & time.
            message = "[" + System.DateTime.Now + "] " + message;

            // Log the message to all logging devices.
            foreach (Interfaces.Log log in logs)
            {
                log.Log(message);
            }
            history.AddLast(message);
            if (history.Count > HistorySize)
                history.RemoveFirst();
        }

        /// <summary>
        ///     Logs an object's state to the logging devices.
        /// </summary>
        /// <param name="message">
        ///     The object under consideration.
        /// </param>
        public void Log(object message)
        {
            Log(message.ToString());
        }
        #endregion
    
    }
}
